define(['moment'], function (moment) {
    return ['$scope', '$filter', '$mdDialog', 'config', 'MMWService', '$calculater', '$gallery', '$timeout',
        function ($scope, $filter, $mdDialog, config, $MMWService, $calculater, $gallery, $timeout) {
            $scope.init = function () {
                var sys_day_range = config.cache.sys_parameter.RuntimeRenewDayRange * -1;
                $scope.query_condition = {};
                $scope.query_condition.area_no = "";        //區域編號
                $scope.query_condition.equipment_no = "";   //設備編號
                $scope.query_condition.over_status = "1";   //僅超標顯示
                $scope.query_condition.report_start_time = moment().add(sys_day_range, 'days').format('YYYY-MM-DD');//開始時間
                $scope.query_condition.report_end_time = moment().format('YYYY-MM-DD'); //結束時間
                $scope.query_condition.light_list = [false, false, true];
                $scope.select_all = false;

                $scope.list_filter = {};
                $scope.filterSH = {};

                $scope.prd_runtime_renew = [];
                if (config.cache.sys_parameter.IsRenewDispatchTime)
                    $scope.is_dispatch_time = config.cache.sys_parameter.IsRenewDispatchTime;
                else
                    $scope.is_dispatch_time = "1";
            };
            //設備工時更新作業-查詢
            $scope.search = function () {
                $scope.select_all = false;
                $scope.prd_runtime_renew = [];
                $scope.list_filter = {};
                $scope.filterSH = {};

                get_equipment_run_data($scope.query_condition, function (data) {
                    if (data.prd_runtime_renew) {
                        $scope.prd_runtime_renew = data.prd_runtime_renew;
                        var is_select_len = $filter("filter")($scope.prd_runtime_renew, { need_select: true }).length;
                        if ($scope.prd_runtime_renew.length == is_select_len && $scope.prd_runtime_renew.length > 0)
                            $scope.select_all = true;
                        else
                            $scope.select_all = false;
                        if ($scope.prd_runtime_renew.length == 0)
                            $scope.$root.showAlert($filter('translate')('common.msg.no_data'));
                    }
                });
            };
            //設備工時更新作業-送出
            $scope.confirm = function () {
                var is_select_data = $filter("filter")($scope.prd_runtime_renew, { need_select: true });
                if (is_select_data.length > 0) {
                    var content = {};
                    content.prd_runtime_renew_info = is_select_data;
                    console.log(is_select_data);
                    run_equipment_time_process(content, function (data) {
                        //執行成功
                        $scope.$root.showToast($filter('translate')('common.msg.success'));
                        $scope.search($scope.query_condition);
                    })
                }
            }
            $scope.back = function () {
                $scope.$root.hideMenu();
                $scope.$root.changeProgram('home', true);
            };
            $scope.select_all_item = function () {
                $scope.prd_runtime_renew.forEach(function (item) {
                    if ($scope.select_all)
                        item.need_select = false;
                    else
                        item.need_select = true;
                });
                if ($scope.select_all)
                    $scope.select_all = false;
                else
                    $scope.select_all = true;

            }
            $scope.item_select = function (item) {
                if (item.need_select)
                    item.need_select = false;
                else
                    item.need_select = true;

                var is_select_len = $filter("filter")($scope.prd_runtime_renew, { need_select: true }).length;
                if ($scope.prd_runtime_renew.length == is_select_len && $scope.prd_runtime_renew.length > 0)
                    $scope.select_all = true;
                else
                    $scope.select_all = false;
            }

            $scope.light_change = function (code) {
                var has_status = 0;
                for (var i in $scope.query_condition.light_list) {
                    if ($scope.query_condition.light_list[i])
                        has_status++
                }
                if (has_status == 1 && $scope.query_condition.light_list[code]) {
                    $scope.$root.showAlert($filter('translate')('IIoT0102.alert_light_change_has_one'));
                    return;
                }
                if (code >= 0 && code < $scope.query_condition.light_list.length) {
                    if ($scope.query_condition.light_list[code])
                        $scope.query_condition.light_list[code] = false;
                    else
                        $scope.query_condition.light_list[code] = true;
                }
            }
            $scope.light_status = function (item) {
                var str = "status_0";
                switch (item.color_status) {
                    case "0":
                        str = "status_0";
                        break;
                    case "1":
                        str = "status_1";
                        break;
                    case "2":
                        str = "status_2";
                        break;
                }
                return str;
            }
            $scope.load_area_no = function () {
                $MMWService.showQueryListPNIG({
                    page_mode: "equipment_run_time_area_get",
                    key: 'page_no',
                    header: [
                        { label: $filter('translate')('common.queryList.area_c'), code: 'page_no' },
                        { label: $filter('translate')('common.queryList.area_n'), code: 'page_name' }
                    ],
                    selectCode: $scope.query_condition.area_no,
                    setCondition: function (conditions) {
                        //conditions.push({column_name:'MASTERNO', query_mode:'0', value:config.cache.account, merge_condition_model:'AND' });
                        return conditions;
                    },
                    confirm: function (selectItem, dialog) {
                        $scope.query_condition.area_no = selectItem.page_no;
                    }
                });
            };

            $scope.load_equipment_no = function () {
                $MMWService.showQueryListPNIG({
                    page_mode: "equipment_run_time_no_get",
                    key: 'page_no',
                    header: [
                        { label: $filter('translate')('common.queryList.machine_c'), code: 'page_no' },
                        { label: $filter('translate')('common.queryList.machine_n'), code: 'page_name' }
                    ],
                    selectCode: $scope.query_condition.equipment_no,
                    setCondition: function (conditions) {
                        //conditions.push({column_name:'MASTERNO', query_mode:'0', value:config.cache.account, merge_condition_model:'AND' });
                        return conditions;
                    },
                    confirm: function (selectItem, dialog) {
                        $scope.query_condition.equipment_no = selectItem.page_no;
                    }
                });
            };

            function get_equipment_run_data(content, feedback) {
                $MMWService.sendToServer({
                    uri: 'ServicesSTD.Module_IIoT.equipment_run_time_info_get',
                    content: content,
                    success: function (data) {
                        feedback(data);
                    }
                });
            }
            function run_equipment_time_process(content, feedback) {
                $MMWService.sendToServer({
                    uri: 'ServicesSTD.Module_IIoT.equipment_run_time_process',
                    content: content,
                    success: function (data) {
                        feedback(data);
                    }
                });
            }
        }
    ];
});
